/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package times.persistence.controllers;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.PriceSourceSlaughterHeader;
import times.persistence.entities.CategoryClass;
import times.persistence.entities.PriceSlaughter;

/**
 *
 * @author Administrator
 */
public class PriceSlaughterJpaController implements Serializable {

    public PriceSlaughterJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PriceSlaughter priceSlaughter) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderId = priceSlaughter.getPriceSourceSlaughterHeaderId();
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId = em.getReference(priceSourceSlaughterHeaderId.getClass(), priceSourceSlaughterHeaderId.getId());
                priceSlaughter.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeaderId);
            }
            CategoryClass categoryClassId = priceSlaughter.getCategoryClassId();
            if (categoryClassId != null) {
                categoryClassId = em.getReference(categoryClassId.getClass(), categoryClassId.getId());
                priceSlaughter.setCategoryClassId(categoryClassId);
            }
            em.persist(priceSlaughter);
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId.getPriceSlaughterList().add(priceSlaughter);
                priceSourceSlaughterHeaderId = em.merge(priceSourceSlaughterHeaderId);
            }
            if (categoryClassId != null) {
                categoryClassId.getPriceSlaughterList().add(priceSlaughter);
                categoryClassId = em.merge(categoryClassId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PriceSlaughter priceSlaughter) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PriceSlaughter persistentPriceSlaughter = em.find(PriceSlaughter.class, priceSlaughter.getId());
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderIdOld = persistentPriceSlaughter.getPriceSourceSlaughterHeaderId();
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderIdNew = priceSlaughter.getPriceSourceSlaughterHeaderId();
            CategoryClass categoryClassIdOld = persistentPriceSlaughter.getCategoryClassId();
            CategoryClass categoryClassIdNew = priceSlaughter.getCategoryClassId();
            if (priceSourceSlaughterHeaderIdNew != null) {
                priceSourceSlaughterHeaderIdNew = em.getReference(priceSourceSlaughterHeaderIdNew.getClass(), priceSourceSlaughterHeaderIdNew.getId());
                priceSlaughter.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeaderIdNew);
            }
            if (categoryClassIdNew != null) {
                categoryClassIdNew = em.getReference(categoryClassIdNew.getClass(), categoryClassIdNew.getId());
                priceSlaughter.setCategoryClassId(categoryClassIdNew);
            }
            priceSlaughter = em.merge(priceSlaughter);
            if (priceSourceSlaughterHeaderIdOld != null && !priceSourceSlaughterHeaderIdOld.equals(priceSourceSlaughterHeaderIdNew)) {
                priceSourceSlaughterHeaderIdOld.getPriceSlaughterList().remove(priceSlaughter);
                priceSourceSlaughterHeaderIdOld = em.merge(priceSourceSlaughterHeaderIdOld);
            }
            if (priceSourceSlaughterHeaderIdNew != null && !priceSourceSlaughterHeaderIdNew.equals(priceSourceSlaughterHeaderIdOld)) {
                priceSourceSlaughterHeaderIdNew.getPriceSlaughterList().add(priceSlaughter);
                priceSourceSlaughterHeaderIdNew = em.merge(priceSourceSlaughterHeaderIdNew);
            }
            if (categoryClassIdOld != null && !categoryClassIdOld.equals(categoryClassIdNew)) {
                categoryClassIdOld.getPriceSlaughterList().remove(priceSlaughter);
                categoryClassIdOld = em.merge(categoryClassIdOld);
            }
            if (categoryClassIdNew != null && !categoryClassIdNew.equals(categoryClassIdOld)) {
                categoryClassIdNew.getPriceSlaughterList().add(priceSlaughter);
                categoryClassIdNew = em.merge(categoryClassIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = priceSlaughter.getId();
                if (findPriceSlaughter(id) == null) {
                    throw new NonexistentEntityException("The priceSlaughter with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PriceSlaughter priceSlaughter;
            try {
                priceSlaughter = em.getReference(PriceSlaughter.class, id);
                priceSlaughter.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The priceSlaughter with id " + id + " no longer exists.", enfe);
            }
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderId = priceSlaughter.getPriceSourceSlaughterHeaderId();
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId.getPriceSlaughterList().remove(priceSlaughter);
                priceSourceSlaughterHeaderId = em.merge(priceSourceSlaughterHeaderId);
            }
            CategoryClass categoryClassId = priceSlaughter.getCategoryClassId();
            if (categoryClassId != null) {
                categoryClassId.getPriceSlaughterList().remove(priceSlaughter);
                categoryClassId = em.merge(categoryClassId);
            }
            em.remove(priceSlaughter);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PriceSlaughter> findPriceSlaughterEntities() {
        return findPriceSlaughterEntities(true, -1, -1);
    }

    public List<PriceSlaughter> findPriceSlaughterEntities(int maxResults, int firstResult) {
        return findPriceSlaughterEntities(false, maxResults, firstResult);
    }

    private List<PriceSlaughter> findPriceSlaughterEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(PriceSlaughter.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PriceSlaughter findPriceSlaughter(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PriceSlaughter.class, id);
        } finally {
            em.close();
        }
    }

    public int getPriceSlaughterCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<PriceSlaughter> rt = cq.from(PriceSlaughter.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
